<?php
	header("Content-Type: text/html; charset=iso-8859-1");

	session_start();
	if(!$_SESSION['logged']){
		header("Location: index.php");
		exit;
	}

    require_once('include/db_connection.php');
    require_once('include/script_includes.php');
    require_once('include/config.inc.php');

	if (strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') == false && strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') == false && strstr($_SERVER['HTTP_USER_AGENT'],'iPod') == false)
	{
		print("Bitte einen guten Browser benutzen!!!");
		exit;
	}

    $debugModus = false;
    if (isset($_GET['debug']) == true) {
        $debugModus = true;
    }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>MMS-Dart-Liga</title>
        <meta content="text/html; charset=iso-8859-1" http-equiv="content-type"/>
        <link rel="stylesheet" type="text/css" href="css/global.css"/>
        <link rel="stylesheet" type="text/css" href="css/jquery.jqplot.min.css"/>
        <link rel="stylesheet" type="text/css" href="css/jquery-ui.css"/>
		<link rel="stylesheet" type="text/css" href="css/jquery.contextMenu.css"/>
		<link rel="stylesheet" type="text/css" href="dist/themes/default/style.min.css" />

        <script type="text/javascript" src="scripts/jquery-2.1.0.min.js"></script>
        <script type="text/javascript" src="scripts/jquery-ui.js"></script>
		<script type="text/javascript" src="scripts/jquery.ui.position.js"></script>
		<script type="text/javascript" src="scripts/jquery.contextMenu.js"></script>
		<script type="text/javascript" src="scripts/jquery.windowmsg-1.0.js"></script>
		<script type="text/javascript" src="scripts/jquery.hoverIntent.minified.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jquery.jqplot.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.highlighter.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.cursor.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.dateAxisRenderer.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.canvasTextRenderer.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.canvasAxisTickRenderer.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.categoryAxisRenderer.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.BezierCurveRenderer.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.trendline.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.dragable.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.pieRenderer.min.js"></script>
		<script type="text/javascript" src="scripts/jqplot/jqplot.ohlcRenderer.min.js"></script>
		<script type="text/javascript" src="scripts/jqplot/jqplot.barRenderer.min.js"></script>
		<script type="text/javascript" src="scripts/jqplot/jqplot.pointLabels.min.js"></script>
        <script type="text/javascript" src="scripts/main.js"></script>
        <script type="text/javascript" src="scripts/dartautomat.js"></script>
		<script type="text/javascript" src="dist/jstree.min.js"></script>
		<script type="text/javascript" src="scripts/jquery.iosslider.js"></script>

    </head>
    <body>
        <div id="pageWrapper">
            <div id="pageHeader">

            </div>
            <div id="contentWrapper">
                <?php echo startDebugInfo($debugModus);?>
                <div id="contentHeader">
                    <div id="siteTitle">
                        <h1>MMS-Dart-Liga</h1>
                        <?php echo "<div class='user'><a href='logout.php'>".$_SESSION['fname']." ".$_SESSION['lname']." (".$_SESSION['role'].")</a> <a href='statistics.php' onclick='goStatistics(this);'>Dashboard</a></div>";
                            /*require_once('include/navi.php');*/
                        ?>
                        <input type="hidden" id="userid" name="user_id" value="<?=$_SESSION['user_id']?>">
                        <input type="hidden" id="manr" name="manr" value="<?=$_SESSION['manr']?>">
                    </div>
                    <?php
                        if (isset($_GET['kw']) == true) {
                            $yearkwselected = $_GET['kw'];
                        } else {
                            $yearkwselected = date('Y') . ltrim(date('W'), '0');
                        }
                        $manr = $_SESSION['manr'];
                    ?>

                    <div id="weekSwitcher" data-currentWeek="<?php echo $yearkwselected;?>">
                        <?php

                        $res = $mysqli->query("select * from view_all_kw");
                        print("<form action='" . $_SERVER['PHP_SELF'] . "'>");
                        print("<select onchange='this.form.submit()' name='kw' size='1'>");
                        print("<option value=\"all\">Gesamt</option>");
                        while (($row = $res->fetch_assoc()) != false) {
                            if ($row['kw2'] == $yearkwselected) {
                                print("<option selected value=" . $row['kw2'] . ">" . $row['kw'] . "</option>");
                            } else {
                                print("<option value=" . $row['kw2'] . ">" . $row['kw'] . "</option>");
                            }
                        }
                            $res->close();
                        print("</select>");
                        print("</form>");
                        ?>
                    </div>
                    <div id="navi">
                        <div class='iosSlider'>
                                <div class='slider'>
                                        <div class = 'item'>News</div>
                                        <div class = 'item'>Dartz&auml;hler:
                                                <?php
                                                        $sql = "select count(*) count from game_score_v2 where manr = " .$_SESSION['manr'];
                                                        print($mysqli->query($sql)->fetch_object()->count);
                                                ?>
                                        </div>
                                        <div class = 'item'>Top Score: <?php// echo getTopScore($manr)?></div>
                                        <div class = 'item'>Top 3-Dart-Average: <?php// echo getTopAvg()?></div>
                                </div>
                        </div>
                        <div class="statisticLayer"><div style='float:right;display:block;width:250px;height:200px;margin:20px;' id='chartOverlay1'></div><div style='display:block;width:330px;height:200px;margin:20px;' id='chartOverlay2'></div></div>
                        <div id='livelink'></div>
                    </div>
                </div>

                <?php echo endDebugInfo($debugModus);?>
                <div id="content">
                    <div class="contentLeft">
                        <?php echo startDebugInfo($debugModus);?>
                            <?php

                        // save game
                                            $rndGame = rand(1, 20);
                                            if(isset($_GET['playerid'])) {
                                                    $players = $_GET['playerid'];
                                            } else {
                                                    $players = null;
                                            }

                        game_save($mysqli,null,$_SESSION['role'],$players,$rndGame,$manr);
                        if (isset($_GET['savegame']) == true) {
                            $res = $mysqli->query("select max(id) max_game_id from game_data where manr = " .$manr);
                            $row = $res->fetch_assoc();
                            $id = $row['max_game_id'] + 1;
                            foreach ($_GET as $key => $value) {
                                if (startsWith($key, 'player_') && $value != "") {
                                    $tmpArr = explode("_", $key);
                                    $player_id = $tmpArr[1];
                                    $sql = "INSERT INTO game_data (manr, id,date, game_id, player_id, score) VALUES (".$manr."," . $id . ",CURDATE(), " . $_GET['game_id'] . "," . $player_id . "," . $value . ")";
                                    mysqli_query($mysqli, $sql);
                                    $mysqli->commit();
                                }
                            }
                        }
                        ?>
                        <?php ?>
                        <?php echo endDebugInfo($debugModus);?>
                    </div>
                    <div class="contentMiddle">
                        <?php echo startDebugInfo($debugModus);?>
                        <?php
                            if ($yearkwselected != "all" && $yearkwselected != "all") {
                                games_kw_summary($mysqli, $yearkwselected, $yearkwselected,$manr);

                            } else{
                                games_summary($mysqli,$manr);
                            }
                        ?>
                        <?php echo endDebugInfo($debugModus);?>
                        <div class="iosSliderCheckouts">
                            <div class="slider">
                                <div class="item">
                                    <?php echo startDebugInfo($debugModus);?>
                                    <?php
                                        getCountScores($mysqli,$_SESSION['role'],$manr);
                                    ?>
                                    <?php echo endDebugInfo($debugModus);?>
                                </div>
                                <div class="item">
                                    <?php echo startDebugInfo($debugModus);?>
                                    <?php
                                        //getRandomGame($mysqli,$_SESSION['role'],null);

                                        getBetsStatistics($mysqli,$_SESSION['role'],$manr);
                                    ?>
                                    <?php echo endDebugInfo($debugModus);?>
                                </div>
								<div class="item">
                                    <?php echo startDebugInfo($debugModus);?>
                                    <?php
                                        getHighFinishStatistics($mysqli,$_SESSION['role'],$manr);
                                    ?>
                                    <?php echo endDebugInfo($debugModus);?>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="contentRight">
                        <?php echo startDebugInfo($debugModus);?>
                        <?php
                            getTopScores($mysqli,$_SESSION['role'],$manr);
                        ?>
                        <?php echo endDebugInfo($debugModus);?>
                        <?php echo startDebugInfo($debugModus);?>
                        <?php
                            getTopAvgs($mysqli,$_SESSION['role'],$manr);
                        ?>
                        <?php echo endDebugInfo($debugModus);?>
                        <?php echo startDebugInfo($debugModus);?>
                        <?php
                            $kwWinners = getKwWinners(27,$manr);
                            if (is_array($kwWinners) and count($kwWinners) > 0)
                            {
                                ?>
                                <div class="kwWinners scrollbarover">
                                    <table class="gridtable">
                                        <tr>
                                                <th colspan="3">Wochensieger</th>
                                        </tr>
                                        <?php
                                        foreach($kwWinners as $kwWinner) {
                                            if (is_array($kwWinner) and count($kwWinner) > 0) {
                                                foreach($kwWinner as $userTemp) {
                                                    if (isset($userTemp['player_id'])) {
                                                        ?>
                                                        <tr>
                                                                <td class="color_<?=$userTemp['player_id']?>"><?= $userTemp['firstname']. " " . $userTemp['lastname']?></td>
                                                                <td><?= $userTemp['kw']. " - " . $userTemp['year']?></td>
                                                                <td><?= round($userTemp['score_abs'], 2);?></td>
                                                        </tr>
                                                        <?php
                                                    }
                                                }
                                            }
                                        }
                                        ?>
                                    </table>
                                </div>
                                <?php
                            }
                        ?>
                        <?php echo endDebugInfo($debugModus);?>
                    </div>
                    <div class="clear"></div>

                    <div class="contentFull">
                    <?php echo startDebugInfo($debugModus);?>
                    <?php
                        games_table($mysqli, $yearkwselected, null, null,$manr);

                        if (isset($_GET['debug']) == true) {
                            foreach ($_GET as $key => $value) {
                                echo "\$_GET['$key'] = $value<br/>";
                            }
                        }
                    ?>
                    <?php echo endDebugInfo($debugModus);?>
                    </div>
                </div>
            </div>
            <div id="pageFooter">
			</div>
            <div id="statisticsDialog"></div>
            <div id="jsOverlays">
                <div id="addNewPlayer" class="jsOverlayBox">
                    <div class="overlayBoxCloseContainer"><a href="#" class="jsOverlayCloseButton">X</a></div>
                    <div class="overlayBoxHeadline">Spieler hinzufügen</div>
                    <div class="overlayBoxContent">TEST</div>
                </div>
            </div>
        </div>
    </body>
</html>